package com.sharemarking.system.controller;

import com.sharemarking.system.dto.Page;
import com.sharemarking.system.dto.orgVerify.OrgVerifyListDto;
import com.sharemarking.system.dto.personManagement.PersonListDto;
import com.sharemarking.system.mapper.OrgVerifyMapper;
import com.sharemarking.system.mapper.PersonManagementMapper;
import com.sharemarking.wa.common.entity.OrgVerify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Controller
@RequestMapping("system/dispatcherManagement")
public class DispatcherManagementController {
    @Autowired
    private PersonManagementMapper pm;
    @Autowired
    private HttpSession session;

    /**
     * 列表页面
     * @param
     * @param model
     */
    @GetMapping("index")
    @PreAuthorize("hasPermission('base','all')")
    public String index(PersonListDto obj, Integer page, Model model, Integer isSession){
        int size=10;
        if(page==null) {
            page=0;
        }
        // session 参数回显
        if (isSession != null) {
            // 如果isSession为1，代表要从session取条件参数，一般应用在某操作之后要保存查询参数
            obj = session.getAttribute("query") == null ? obj : (PersonListDto) session.getAttribute("query");
            size = session.getAttribute("size") == null ? size : (int) session.getAttribute("size");
            page = session.getAttribute("page") == null ? page : (int) session.getAttribute("page");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("pageIndex",page*size);
        map.put("pageSize",size);

        if(!StringUtils.isEmpty(obj.getMemberName())) {
            map.put("memberName",obj.getMemberName());
        }
        if(!StringUtils.isEmpty(obj.getMobile())) {
            map.put("mobile",obj.getMobile());
        }
        if(!StringUtils.isEmpty(obj.getOrgName())) {
            map.put("orgName",obj.getOrgName());
        }
        List<PersonListDto> list=pm.getDispatcherList(map);
        int total = pm.getDispatcherListTotal(map);
        model.addAttribute("page", new Page<>(page, size, total, list));
        model.addAttribute("query",obj);
        // session 参数保存
        session.setAttribute("query",obj);
        session.setAttribute("size", size);
        session.setAttribute("page", page);
        return "personManagement/dispatcherManagementIndex";
    }

}
